<?php
// 链接数据库
include_once('config/config.php');

if($_POST)
 {
   $action = $_POST['action'];
 
   if($action == 'del')
   {
     $id = $_POST['id'];
 
     $admin = find("SELECT * FROM `pre_job` WHERE `id` = '$id'");
	 
     if(!$admin)
     {
       error('该职位无法删除');
     }
 
     // 登录的管理员是否是被删除 没有登录功能
 
     $result = delete('job',"`id` = '$id'");
 
     if($result)
     {
       success('删除成功');
     }else{
       error('删除失败');
     }
     
   }
 }

// 执行的sql语句
$sql = 'SELECT j.id, j.name, d.name AS department FROM pre_job AS j LEFT JOIN pre_department AS d ON j.depid = d.id;';
// 调用封装的函数
$result = leftjoin($sql);
// pre($result);

?>
<!DOCTYPE html>
<html lang="en">
  <head>
	  <!--  -->
    <?php include_once('common/meta.php'); ?>
	
  </head>

  <body> 
    
   <?php include_once('common/header.php'); ?>
    
    <?php include_once('common/sidebar.php'); ?>
	
    <div class="content">
        <div class="header">
            <h1 class="page-title">职位列表</h1>
        </div>
        <ul class="breadcrumb">
            <li><a href="index.php">Home</a> <span class="divider">/</span></li>
            <li class="active">job</li>
        </ul>

        <div class="container-fluid">
            <div class="row-fluid">
                <div class="btn-toolbar">
                    <button class="btn btn-primary" onClick="location='jobadd.php'"><i class="icon-plus"></i>新增职位</button>
                </div>
                <div class="well">
                    <table class="table">
                      <thead>
                        <tr>
                          <th>ID</th>
                          <th>职位名称</th>
                          <th>部门</th>
                          <th style="width: 26px;"></th>
                        </tr>
                      </thead>
                      <tbody>
						<?php foreach($result as $item){ ?>
                        <tr>
                          <td><?php echo $item['0'] ?></td>
                          <td><?php echo $item['1'] ?></td>
                          <td><?php echo $item['2'] ?></td>
                          <td>
                              <a href="jobup.php?id=<?php echo $item['0'] ?>"><i class="icon-pencil"></i></a>
                              <a href="#myModal" class="del" data-id="<?php echo $item['0'] ?>" role="button" data-toggle="modal"><i class="icon-remove"></i></a>
                          </td>
                        </tr>
						<?php } ?>
                      </tbody>
                    </table>
                </div>
                <div class="pagination">
                    <ul>
                        <li><a href="#">Prev</a></li>
                        <li><a href="#">1</a></li>
                        <li><a href="#">2</a></li>
                        <li><a href="#">3</a></li>
                        <li><a href="#">4</a></li>
                        <li><a href="#">Next</a></li>
                    </ul>
                </div>

                <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h3 id="myModalLabel">Delete Confirmation</h3>
                    </div>
                    <div class="modal-body">
                        <p class="error-text"><i class="icon-warning-sign modal-icon"></i>Are you sure you want to delete the user?</p>
                    </div>
                    <div class="modal-footer">
                        <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
                        <button class="btn btn-danger" data-id="" data-dismiss="modal">Delete</button>
                    </div>
                </div>

                <?php include_once('common/footer.php'); ?>
            </div>
        </div>
    </div>
    
   <?php include_once('common/script.php'); ?>
  </body>
</html>
<script>
	$('.del').click(function(){
		let id = $(this).data('id');
		$('.btn-danger').attr('data-id',id);
	})
	
	$('.btn-danger').click(function(){
	    let id = $(this).data('id')
	
	    // 发起异步请求
	    $.ajax({
	      // 请求方法 get post 一般都是用post
	      type:'post',
	      // 请求地址
	      url:'joblist.php',
	      // 请求需要的参数
	      data:{
	        id, //id:id
	        action:'del'
	        // id:5
	      },
	      // 请求返回的数据类型
	      dataType:'json',// html text xml json
	      // 请求成功的回调函数 http 状态码 = 200 
	      success:function(res)
	      {
	        if(res.code === 1)
	        {
	          alert(res.msg)
	          location.reload()
	        }else{
	          alert(res.msg)
	        }
	      },
	      // 请求失败的回调函数 404 301 500等
	      error:function(error)
	      {
	        console.log(error)
	
	      }
	
	    })
	
	  })
</script>

